<template>
  <div class="container">
    <!-- 轮播图 -->
    <div class="block">
      <el-carousel :interval="5000" arrow="always">
        <el-carousel-item v-for="(item,index) in banners" :key="index">
          <img :src="$axios.defaults.baseURL+item.url" alt />
        </el-carousel-item>
      </el-carousel>
    </div>
    <!-- 搜索框 -->
    <div class="banner-content">
      <div class="search-bar">
        <!-- tab栏 -->
        <el-row type="flex" class="search-tab">
          <span
            class="index"
            :class="{active:index==currentIndex}"
            v-for="(value,index) in tabs"
            :key="index"
            @click="dealTabChange(index)"
          >
            <i>{{value.name}}</i>
          </span>
        </el-row>

        <!-- 输入框 -->
        <el-row type="flex" align="middle" class="search-input">
          <input :placeholder="tabs[currentIndex].desc" v-model="userKey" @keyup.enter="searchJump" />
          <i class="el-icon-search" @click="searchJump"></i>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      banners: [],   // 轮播图数据
      tabs: [
        { name: '攻略', desc: '搜索城市', url: '/post?city=' },        //示例路径： http://157.122.54.189:9093/post?city=aa&start=0&limit=3
        { name: '酒店', desc: '请输入城市搜索酒店', url: '/hotel?city=' },
        { name: '机票', desc: '请输入出发地', url: '/air' }
      ],
      currentIndex: 0,       //用来标记当前点击的span （当前点击选中的索引）
      userKey: ''                //搜索框搜索关键字 
    }
  },
  async mounted() {
    //发送请求，获取轮播图数据
    let res = await this.$axios({
      url: "/scenics/banners"
    })
    console.log(res);
    this.banners = res.data.data;
    // 方法2：
    // this.$axios({
    //   url: "/scenics/banners"
    // })
    //   //请求成功
    //   .then(res => {
    //     console.log(res);
    //     this.banners = res.data.data;
    //   })
  },
  methods: {
    //切换标签栏
    dealTabChange(index) {
      this.currentIndex = index;   //实现标记当前点击的span
      //如果点击了 机票，进行页面跳转
      let current = this.tabs[index];
      if (current.name == '机票') {
        this.$router.push({ path: current.url })
      }
    },
    //搜索事件
    searchJump() {
      // console.log(this.currentIndex);   //0  1  点击了tabs栏，再点击搜索能得到tabs栏的索引值
      let item = this.tabs[this.currentIndex];
      //注意跳转的链接来自当前选中的`tab`的`pageUrl`属性，并且附带上参数
      this.$router.push({ path: item.url + this.userKey })  //示例路径： http://157.122.54.189:9093/post?city=aa&start=0&limit=3
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  min-width: 1000px;
  margin: 0 auto;
  position: relative;

  /deep/ .el-carousel__container {
    height: 700px;
  }

  .banner-image {
    width: 100%;
    height: 100%;
  }
  // 搜索块样式
  .banner-content {
    z-index: 9;
    width: 1000px;
    position: absolute;
    left: 50%;
    top: 45%;
    margin-left: -500px;
    border-top: 1px transparent solid;

    .search-bar {
      width: 552px;
      margin: 0 auto;
    }

    .search-tab {
      .active {
        i {
          color: #333;
        }
        &::after {
          background: #eee;
        }
      }

      span {
        width: 82px;
        height: 36px;
        display: block;
        position: relative;
        margin-right: 8px;
        cursor: pointer;

        i {
          position: absolute;
          z-index: 2;
          display: block;
          width: 100%;
          height: 100%;
          line-height: 30px;
          text-align: center;
          color: #fff;
        }

        &:after {
          position: absolute;
          left: 0;
          top: 0;
          display: block;
          content: "";
          width: 100%;
          height: 100%;
          border: 1px rgba(255, 255, 255, 0.2) solid;
          border-bottom: none;
          transform: scale(1.1, 1.3) perspective(0.7em) rotateX(2.2deg);
          transform-origin: bottom left;
          background: rgba(0, 0, 0, 0.5);
          border-radius: 1px 2px 0 0;
          box-sizing: border-box;
        }
      }
    }

    .search-input {
      width: 550px;
      height: 46px;
      background: #fff;
      border-radius: 0 4px 4px 4px;
      border: 1px rgba(255, 255, 255, 0.2) solid;
      border-top: none;
      box-sizing: unset;

      input {
        flex: 1;
        height: 20px;
        padding: 13px 15px;
        outline: none;
        border: 0;
        font-size: 16px;
      }

      .el-icon-search {
        cursor: pointer;
        font-size: 22px;
        padding: 0 10px;
        font-weight: bold;
      }
    }
  }
}
</style>
